package edu.avans.hartigehap.web.controller;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.*;
import org.springframework.validation.*;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;

import org.apache.commons.io.IOUtils;
import edu.avans.hartigehap.web.form.*;

import javax.annotation.PostConstruct;
import javax.servlet.http.Part;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import com.google.common.collect.Lists;
import edu.avans.hartigehap.web.util.*;

import java.util.*;

import edu.avans.hartigehap.domain.*;
import edu.avans.hartigehap.service.*;

import javax.servlet.http.*;

@RequestMapping("/")
@Controller
public class WebDishController {
	final Logger logger = LoggerFactory.getLogger(WebDishController.class);

	@Autowired
	private MessageSource messageSource;
	@Autowired
	private WebDishService webDishService;
	@Autowired
	private RestaurantService restaurantService;

	@RequestMapping(value = "restaurants/{restaurantName}/webdishes", method = RequestMethod.GET)
	public String listWebdishes(
			@PathVariable("restaurantName") String restaurantName, Model uiModel) {

		// warmup stuff

		logger.info("Listing webdishes");
		List<WebDish> webdishes = webDishService.findAll();
		uiModel.addAttribute("webdishes", webdishes);
		logger.info("No. of webdishes: " + webdishes.size());

		logger.info("erco normal: " + webdishes);

		return "hartigehap/listwebdishes";
	}

	@PostConstruct
	public void createWebDish() {
		webDishService.createNewWebMenuItem();
	}

	@RequestMapping(value = "webdishes/{id}", method = RequestMethod.GET)
	public String showWebdish(@PathVariable("id") Long id, Model uiModel) {
		WebDish webdish = webDishService.findById(id);
		uiModel.addAttribute("webdish", webdish);
		return "hartigehap/showwebdish";
	}

	@RequestMapping(value = "dishes/{id}", params = "delete", method = RequestMethod.GET)
	public String delete(@PathVariable("id") Long id) {
		logger.info("Deleting webdish");
		webDishService.delete(id);
		return "redirect:/webdishes/";
	}

	// Pizza details

	@RequestMapping(value = { "/pizzadetails" }, method = RequestMethod.GET)
	public String listPizzaDetails(Model uiModel) {
		Collection<WebMenuItem> dishes = webDishService.findAllPizzas();
		uiModel.addAttribute("dishes", dishes);
		return "hartigehap/pizzadetails";
	}

	@RequestMapping(value = "/pizzadetails/{id}", method = RequestMethod.POST)
	public String deletePizza(@PathVariable("id") Long id) {
		logger.info("Deleting pizza");
		webDishService.deleteMenuItem(id);
		return "redirect:/pizzadetails/";
	}

	@RequestMapping(value = "/pizzachange/{id}", method = RequestMethod.GET)
	public String editPizzaPage(@PathVariable("id") Long id,
			Model uiModel) {
		System.out.println("TestEdit");
		logger.info("Editing page");
		WebMenuItem menuItem = webDishService.findMenuItemById(id);
		uiModel.addAttribute("menuItem", menuItem);
		return "hartigehap/pizzachange";
	}

	@RequestMapping(value = "/pizzachange/{id}", method = RequestMethod.POST)
	public String editPizza(@PathVariable("id") Long id,
			Model uiModel, String dishName, BigDecimal price) {
		System.out.println("TestEdit2");
		logger.info("Editing pizza");
		WebMenuItem webMenuItem = webDishService.findMenuItemById(id);
		webMenuItem.setDishName(dishName);
		webMenuItem.setPrice(price);
		webDishService.saveMenuItem(webMenuItem);
		return "redirect:/pizzadetails/";
	}

	@RequestMapping(value = { "/pizzachangenew" }, method = RequestMethod.GET)
	public String newPizza() {
		return "hartigehap/pizzachangenew";
	}

	@RequestMapping(value = "/pizzachangenew", method = RequestMethod.POST)
	public String newPizza(@RequestParam String dishName, BigDecimal price,
			RedirectAttributes redirectAttributes, Model uiModel, Locale locale) {

		logger.info("dishName = " + dishName);
		WebMenuItem webMenuItem = new MenuPizza(price, dishName);
		webDishService.saveMenuItem(webMenuItem) ;
		return "redirect:/pizzadetails/";

	}

}
